package com.iRomantic.domain.entity;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import com.iRomantic.config.Constant;
import com.jfinal.plugin.activerecord.Model;

/**
 * 婚礼
 * @author serenity
 *
 */
public class Wedding extends Model<Wedding>{

	private static final long serialVersionUID = 9093331677540978728L;
	
	public static final Wedding DAO = new Wedding();
	
	public Wedding findByNumber(String number){
		return this.findFirst("select * from t_wedding where number=?", number);
	}
	
	/**
	 * 获取酒店
	 * @return
	 */
	public Hotel getHotel(){
		Hotel hotel = Hotel.DAO.findFirst(" select * from t_hotel where id=? ",getStr("hotelId"));
		if(hotel!=null){
			hotel.setPictureProjectPath(hotel);
		}
		return hotel;
	}
	
	/**
	 * 获取婚庆企业
	 * @return
	 */
	public Enterprise getEnterprise(){
		Enterprise enterprise = Enterprise.DAO.findFirst(" select * from t_enterprise where id=? ", getStr("enterpriseId"));
		if(enterprise!=null){
			enterprise.setOSSPath(enterprise);
		}
		return enterprise;
	}
	
	/**
	 * Save model.
	 */
	public boolean save(){
		if(this.getStr("id")==null){
			this.set("id", UUID.randomUUID().toString());
		}
		this.set("createTime",new Timestamp(System.currentTimeMillis()));
		if(null==getInt("skinType")){
			this.set("skinType", "0");
		}
		return super.save();
	}
	
	/**
	 * 获取精选图片
	 */
	public List<Picture> getMainPictures(){
		List<Picture> list = Picture.DAO.findByWeddingAndType(this.getStr("id"),Picture.TYPE_MAIN);
		Picture.DAO.setOSSPath(list);
		return list;
	}
	
	/**
	 * 获取所有图片列表
	 * @return
	 */
	public List<Picture> getPictures(){
		List<Picture> list = Picture.DAO.findByWedding(this.getStr("id"));
		return list;
	}
	
	/**
	 * 获取视频列表
	 * @return
	 */
	public List<Video> getVideos(){
		List<Video> list = Video.DAO.find("select * from t_video where weddingId=?", this.getStr("id"));
		return list;
	}
	
	/**
	 * 获取请帖
	 * @return
	 */
	public Invitation getInvitation(){
		Invitation invitation = Invitation.DAO.findFirst(" select * from t_invitation where weddingId=?", this.getStr("id"));
		if(invitation!=null){
			invitation.setOSSPath(invitation);
		}
		return invitation;
	}
	
	public void setOSSPath(){
		if(this.getStr("bridegroomUrl")!=null){
			this.set("bridegroomUrl", Constant.OSS_PATH+Constant.WEDDING_PICTURE+this.getStr("id")+"/"+this.getStr("bridegroomUrl"));
		}
		if(this.getStr("brideUrl")!=null){
			this.set("brideUrl", Constant.OSS_PATH+Constant.WEDDING_PICTURE+this.getStr("id")+"/"+this.getStr("brideUrl"));
		}
	}
	

}
